package com.example.springbootkafka.threadpool;

import com.example.springbootkafka.configration.ThreadPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
public class MyTaskExector {
    @Autowired
    private ThreadPoolConfig threadPoolConfig;

    @Bean("myExector")
    public Executor myExector(){
        /**
         * ThreadPoolTaskExecutor是spring包下的，是sring为我们提供的线程池类,放入容器后使用@Async("myExector")即可
         */
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(threadPoolConfig.getCorePoolSize());
        executor.setMaxPoolSize(threadPoolConfig.getMaxPoolSize());
        executor.setKeepAliveSeconds(threadPoolConfig.getKeepAliveSecond());
        executor.setQueueCapacity(threadPoolConfig.getQueueCapacity());
        executor.setThreadNamePrefix("myExector-");
        return executor;
    }
}
